/*
 * ModPass.java
 *
 * Created on 27 de agosto de 2007, 10:58 AM
 *
 * Permite verificar el usuario de Applet, compara la palabra clave
 * proporsionada por el usuario y la cadena de auteticación.
 */

package sim;

import sim.auth.DocAuth;

import sim.auth.DocAuth;
import sim.framework.EventListener;
import sim.framework.Executable;
import sim.framework.Module;
import sim.framework.Msg;
import sim.framework.Resource;
import sim.framework.Simlet;
import sim.framework.dialog.Dialog;
import sim.framework.dialog.DlgForm;
import sim.framework.gui.*;

public class ModPass extends Module {
    
    private static final String CODE_RESET = "*#**#*";
    private static final String CODE_BUILD = "**##**";

    private static final int VALUE_INI_LENGTH = 32;    
    private static final int CODES_MAX_LENGTH = 6;

    private StringBuffer m_Value = new StringBuffer(VALUE_INI_LENGTH);
    private StringBuffer m_Codes = new StringBuffer(CODES_MAX_LENGTH);
    private DocAuth m_DocAuth;
    private DlgForm m_DlgForm;

    public ModPass( int i, Simlet s ) {
        super(i, s);
    }
    
    public void work(final Msg msg) {
	//#ifdef DEBUG_MODPASS
	//# System.out.println("ModPass::work() ENTERED");
	//#endif

	m_DocAuth = (DocAuth)getDocument(Simlet.MOD_AUTH);
	if (m_DocAuth != null) {
	    createGui();
	} else {
	    //#ifdef DEBUG_MODPASS
	    //# System.out.println("\tDocumento no esta cargado!");
	    //#endif
	    execBack();
	}

	//#ifdef DEBUG_MODPASS
	//# System.out.println("ModPass::work() EXITING");
	//#endif
    }
        
    private void createGui(){
        m_DlgForm = (DlgForm)DlgForm.create(this.m_Simlet, GuiCtrl.NUMERIC,
					    getLabel(Resource.LBL_TITLE_APP),
					    null,
					    getLabel(Resource.LBL_MESG_PASS),
					    getLabel(Resource.LBL_CTRL_ACCEPT),
					    null,
					    getLabel(Resource.LBL_CTRL_QUIT));
        setGui(m_DlgForm);
    }

    public boolean event(final Msg msg) {
        boolean ret = false;
        
        switch (msg.getEventCode()) {
	case EventListener.KEY_PRESSED:
	    if(m_DlgForm.popUp()){
		m_DlgForm.popUp_Close();
		m_DlgForm.activeAll(true);
		ret = true;
	    }
	    else {
		switch(msg.getWParam()) {
		case Gui.KEY_STAR:
		case Gui.KEY_POUND:
		    if (m_Codes.length() == CODES_MAX_LENGTH) {
			m_Codes.setLength(0);
		    }
		    execCode(msg.getWParam());
		    ret = true;
		}
	    }
	    break;
	case EventListener.COMMAND:
	    switch (msg.getWParam()) {
	    case Dialog.CTRL_BUTTON_NEXT:
		m_Value.setLength(0);
		m_DlgForm.getText(Dialog.CTRL_MAIN, m_Value);
		m_DocAuth.setPassword(m_Value.toString());
		if (m_DocAuth.isAuthenticated()) {
		    execNext();
		} else {
		    m_DlgForm.setText(m_DlgForm.CTRL_MAIN, "");
		    alert(getLabel(Resource.LBL_MESG_PASS_BAD));
		}
		ret = true;
		break;
	    case Dialog.CTRL_BUTTON_EXIT:
		execBack();
		ret = true;
		break;
	    }
        }

        return ret;
    }
    
    private void alert(final String str){
	m_DlgForm.activeAll(false);
	m_DlgForm.popUp(str, m_Simlet.getFontSys().medium);
    }
    
    private void execCode(int key){
	String string;
        m_Codes.append((key == Gui.KEY_STAR) ? '*' : '#');
	if (m_Codes.length() == CODES_MAX_LENGTH) {
	    string = m_Codes.toString();
	    if (string.equals(CODE_RESET)) {
		exec(createModule(Simlet.MOD_EXIT));
	    }else if (string.equals(CODE_BUILD)){
		alert("Version...");
	    }
	}
    }
}
